package com.system.run.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.run.entity.Patient;
import com.system.run.entity.vo.PatientVo;
import com.system.run.from.Row;
import com.system.run.service.PatientService;
import com.system.run.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-08
 */
@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @ApiOperation("依据UUID查询病人详情")
    @PostMapping("getPatient/{uuid}")
    public Row GetPatient( @ApiParam(name = "uuid", value = "用户标识",required = false) @PathVariable String uuid){
        Patient patient = patientService.getPatientById(uuid);
        return Row.SUCCESS().data("patient",patient);
    }

    @ApiOperation("保存病人详情")
    @PostMapping("savePatient")
    public Row SavePatient( @ApiParam(name = "patient", value = "用户",required = false) @RequestBody Patient patient){
        Patient item = patientService.getPatientById(patient.getId());
        boolean flag = true;
        //添加
        if (item == null){
            patient.setId(UUID.randomUUID().toString());
            flag = patientService.save(patient);
        }
        //修改
        else{
            flag = patientService.updateById(patient);
        }
        if (flag){
            return Row.SUCCESS();
        }else{
            return Row.ERROR();
        }
    }

    @ApiOperation("获取所有病人详情")
    @PostMapping("getPatientList/{current}/{limit}")
    public Row GetPatientList( @ApiParam(name = "current", value = "当前页",required = false) @PathVariable Long current,
                               @ApiParam(name = "limit", value = "每页记录数",required = false) @PathVariable Long limit,
                               @RequestBody(required = false) PatientVo patientVo
                               ){
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(patientVo.getName())){
            queryWrapper.like("name",patientVo.getName());
        }
        if (!StringUtils.isEmpty(patientVo.getSex())){
            queryWrapper.eq("sex",patientVo.getSex());
        }
        if (!StringUtils.isEmpty(patientVo.getAge())){
            queryWrapper.eq("age",patientVo.getAge());
        }
        if (!StringUtils.isEmpty(patientVo.getBuilding())){
            queryWrapper.eq("building",patientVo.getBuilding());
        }
        if (!StringUtils.isEmpty(patientVo.getFloor())){
            queryWrapper.eq("floot",patientVo.getFloor());
        }
        if (!StringUtils.isEmpty(patientVo.getRoom())){
            queryWrapper.eq("room",patientVo.getRoom());
        }
        if (!StringUtils.isEmpty(patientVo.getStatus())){
            queryWrapper.eq("status",patientVo.getStatus());
        }
        if (!StringUtils.isEmpty(patientVo.getStayTime())){
            queryWrapper.ge("stayTime",patientVo.getStayTime());
        }
        if (!StringUtils.isEmpty(patientVo.getExitTime())){
            queryWrapper.le("exitTime",patientVo.getExitTime());
        }
        Page<Patient> patientPage = new Page<>(current,limit);
        patientService.page(patientPage,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",patientPage.getTotal());
        map.put("reconds",patientPage.getRecords());
        return Row.SUCCESS().data(map);
    }
}

